package com.auxgroup.hr.front.site.co.hireperson;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.auxgroup.hr.front.site.vo.person.HireSIRespVO;
import com.auxgroup.hr.front.site.vo.person.HireSISaveReqVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.dao.domain.PsCHirePrimaryClass;
import com.auxgroup.hr.front.dao.domain.PsCHireSiTbl;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.hireperson.HireSiService;
import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.vo.person.HireSIRespVO;
import com.auxgroup.hr.front.site.vo.person.HireSISaveReqVO;

/**
 * 入职人员社保公积金CO
 * @author lvliuhao
 * @version 创建时间：180526 
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/hireSi")
public class HireSiCO extends BaseCO {

	private static final Logger logger = LoggerFactory.getLogger(HireSiCO.class);

	@Autowired
	public HireSiService hireSiService;

	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;

	/**
	 * 获取入职人员社保公积金
	 * @param hireId
	 * @return
	 */
	@GetMapping(value = "/getOne")
	public BaseResponseVO getOne(String rsHireId,BigDecimal cSeqno) {
		logger.info("getHireSi param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(cSeqno==null){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"社保公积金的seqno不能为空");
		}
		PsCHirePrimaryClass hirePrimary = new PsCHirePrimaryClass(cSeqno, rsHireId);
		PsCHireSiTbl hireSi = hireSiService.getHireSi(hirePrimary);
		return buildRespone(hireSi);
	}
	
	/**
	 * 保存入职人员社保公积金
	 * @param hireId
	 * @return
	 */
	@PostMapping(value = "/save")
	public BaseResponseVO save(HireSISaveReqVO saveReqVO) {
		logger.info("saveHireSi param= {}", saveReqVO);
		if(StringUtils.isBlank(saveReqVO.getRsHireId())){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		PsCHireSiTbl hireSi = null;
		if(saveReqVO.getcSeqno()!=null&&saveReqVO.getcSeqno().length>0){
			for (int i = 0; i < saveReqVO.getcSeqno().length; i++) {
				hireSi = new PsCHireSiTbl();
				hireSi.setRsHireId(saveReqVO.getRsHireId());
				if(StringUtils.isNotBlank(saveReqVO.getcSeqno()[i])){
					hireSi.setcSeqno(new BigDecimal(saveReqVO.getcSeqno()[i]));
				}
				hireSi.setcAddress(saveReqVO.getcAddress()[i]);
				hireSi.setcDescr(saveReqVO.getcDescr()[i]);
				hireSi.setcPhfStatus(saveReqVO.getcPhfStatus()[i]);
				hireSi.setcYesNo(saveReqVO.getcYesNo()[i]);
				hireSi.setGpcnAcountNum(saveReqVO.getGpcnAcountNum()[i]);
				hireSiService.save(hireSi);
			}
		}
		return buildRespone(true);
		
	}
	
	/**
	 * 获取全部社保公积金
	 * @param rsHireId
	 * @return
	 */
	@GetMapping(value = "/getAll")
	public BaseResponseVO getAll(String rsHireId){
		logger.info("getHireSi param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		List<PsCHireSiTbl> list = hireSiService.getAll(rsHireId);
		List<Map<String, Object>> afterBuild = buildDictField(list);
		//TODO 转换内容
		HireSIRespVO respVo = new HireSIRespVO();
		respVo.setRsHireId(rsHireId);
		if(afterBuild!=null&&!afterBuild.isEmpty()){
			Integer length = afterBuild.size()>3?3:afterBuild.size();
			for (int i = 0; i < length; i++) {
				respVo.getcAddress()[i]=afterBuild.get(i).get("cAddress")==null?"":afterBuild.get(i).get("cAddress").toString();
				respVo.getcDescr()[i]=afterBuild.get(i).get("cDescr")==null?"":afterBuild.get(i).get("cDescr").toString();
				respVo.getcPhfStatus()[i]=afterBuild.get(i).get("cPhfStatus")==null?"":afterBuild.get(i).get("cPhfStatus").toString();
				respVo.getcPhfStatusValue()[i]=afterBuild.get(i).get("cPhfStatusValue")==null?"":afterBuild.get(i).get("cPhfStatusValue").toString();
				respVo.getcSeqno()[i]=afterBuild.get(i).get("cSeqno")==null?"":afterBuild.get(i).get("cSeqno").toString();
				respVo.getcYesNo()[i]=afterBuild.get(i).get("cYesNo")==null?"":afterBuild.get(i).get("cYesNo").toString();
				respVo.getGpcnAcountNum()[i]=afterBuild.get(i).get("gpcnAcountNum")==null?"":afterBuild.get(i).get("gpcnAcountNum").toString();
			}
		}
		return buildRespone(respVo,false);
	}
	
	

}
